package com.dq.controller.api.merchant.orders;

import com.dq.domain.sys.user.center.UserAccountStaffUser;
import com.dq.exception.customException.AdminException;
import com.dq.controller.in.orders.UserOrderQuery;
import com.dq.vo.ListDataVo;
import com.dq.vo.Vo;
import com.dq.domain.orders.UserOrder;
import com.dq.domain.orders.info.OrderInfo;
import com.dq.domain.sys.user.user.StaffUser;
import com.dq.service.AllService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author heshuaijun
 * @date 2019/9/1716:24
 */
@Api(description = "boss 销售订单管理")
@RestController
@RequestMapping("/sys/boss/userOrder")
public class ApiBossUserOrdersController extends AllService {

    @ApiOperation("销售单列表")
    @PostMapping("/list")
    public Vo list(@RequestBody UserOrderQuery userOrderQuery) throws AdminException {
        userOrderQuery.format();

        List<UserOrder.BasicVo> list = new ArrayList<>();
        List<UserAccountStaffUser> userAccountStaffUserList = cusSaleService.userOrderList(userOrderQuery, false);
        for (UserAccountStaffUser userAccountStaffUser : userAccountStaffUserList) {
//            list.add(userAccountStaffUser.getUserOrder().toBasicVo());
        }
        Long count = cusSaleService.userOrderCount(userOrderQuery, false);

        return new ListDataVo(list, count);
    }

    @GetMapping("/detail/{id}")
    @ApiOperation("销售订单详情商品列表")
    public Vo userOrderDetail(@PathVariable @ApiParam("订单id") Long id) throws AdminException {
        UserOrder userOrder = dao.get(UserOrder.class, id);
        if (null == userOrder) {
            throw new AdminException("该订单不存在");
        }

        List<OrderInfo> orderInfoList = orderInfoRepository.findByOrders(userOrder);
        List<OrderInfo.OrgOrderInfoVo> orgOrderInfoVos = new ArrayList<>();
        orderInfoList.forEach(orderInfo -> {
            orgOrderInfoVos.add(orderInfo.toOrgOrderInfoVo());
        });

        UserOrder.BasicVo basicVo = userOrder.toBasicVo();

        basicVo.setOrgOrderInfoVos(orgOrderInfoVos);

        // 获取关联员工集合
        List<StaffUser.Vo> staffUserVos = new ArrayList<>();
//        List<UserAccountStaffUser> userAccountStaffUserList = userOrderStaffUserRepository.findByUserOrderAndDeletedFalse(userOrder);
        List<UserAccountStaffUser> userAccountStaffUserList = new ArrayList<>();
        for (UserAccountStaffUser userAccountStaffUser : userAccountStaffUserList) {
            staffUserVos.add((StaffUser.Vo) userAccountStaffUser.getStaffUser().toVO());
        }
        basicVo.setStaffUserList(staffUserVos);

        return new Vo(basicVo);
    }

    @GetMapping("/delete/{orderId}")
    @ApiOperation("订单删除")
    public Vo orgUserDelete(@ApiParam("订单ID") @PathVariable Long orderId)throws AdminException{
        UserOrder userOrder = dao.get(UserOrder.class, orderId);
        if (null == userOrder) {
            throw new AdminException("订单不存在");
        }

        userOrder.setDeleted(true);

        userOrderService.update(userOrder);
        return new Vo(HttpStatus.OK);
    }

}